package cn.wsalix.msg.service.impl;

import java.util.HashMap;
import java.util.Map;
import java.util.Observable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.anlaser.service.impl.SimpleServiceImpl;
import cn.anlaser.utils.SearchFilter;
import cn.anlaser.utils.SearchFilter.Operator;
import cn.wsalix.admin.service.UserService;
import cn.wsalix.msg.entity.Message;
import cn.wsalix.msg.form.MessageForm;
import cn.wsalix.msg.repository.MessageRepository;
import cn.wsalix.msg.service.MessageService;
import cn.wsalix.msg.status.MessEnum;
import cn.wsalix.observable.service.SendService;
import cn.wsalix.order.service.OrderService;
import cn.wsalix.order.service.TuanService;
import cn.wsalix.shop.service.ProductService;

@Service("messageService")
public class MessageServiceImpl extends
		SimpleServiceImpl<MessageForm, MessageRepository, Message> implements
		MessageService {
	@Autowired
	private MessageRepository messageRepository;
	@Autowired
	private ProductService productService;
	@Autowired
	private TuanService tuanService;
	@Autowired
	private OrderService orderService;
	@Autowired
	private UserService userService;
	@Override
	public String module() {
		return "/admin/mess";
	}

	@Override
	public Message add(Message entity) {
		entity.setStatus(MessEnum.edit);
		return super.add(entity);
	}

	@Override
	public Message edit(Message entity) {
		entity.setStatus(MessEnum.edit);
		return super.edit(entity);
	}

	@Override
	public MessageRepository getRepository() {
		return messageRepository;
	}

	@Override
	public Class<Message> getEntityClazz() {
		return Message.class;
	}

	@Override
	public Map<String, SearchFilter> buildByForm(MessageForm form) {
		Map<String, Object> searchParams = new HashMap<String, Object>();
		Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
		if (form.getName() != null && !form.getName().trim().equals("")) {
			filters.put("name",
					new SearchFilter("name", Operator.LIKE, form.getName()));
		}
		if (form.getType() != null) {
			filters.put("type",
					new SearchFilter("type", Operator.EQ, form.getType()));
		}
		if (form.getFkId() != null && form.getFkId() != 0) {
			filters.put("fkId",
					new SearchFilter("fkId", Operator.EQ, form.getFkId()));
		}
		return filters;
	}

	@Override
	public Object findObjForm(MessageForm form) {
		if (form == null || form.getType() == null) {
			return null;
		}
		switch (form.getType()) {
		case order:
			return orderService.findById(form.getFkId());
		case product:
			return productService.findById(form.getFkId());
		case tuan:
			return tuanService.findById(form.getFkId());
		case user:
			return userService.findById(form.getFkId());
		default:
			break;
		}
		return null;
	}
	@Transactional
	@Override
	public void update(Observable o, Object arg) {
		if (o instanceof SendService) {
			SendService sendService = (SendService) o;
			if (arg instanceof MessageForm) {
				MessageForm form = (MessageForm) arg;
				Message message=findById(form.getId());
				message.setStatus(MessEnum.sended);
				super.edit(message);
				sendService.setMessage(message);
			}
		}
	}

}
